import 'package:flutter/material.dart';

void main() {
  runApp(const TaxApp());
}

class TaxApp extends StatelessWidget {
  const TaxApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '税务助手',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        useMaterial3: true,
      ),
      home: const MainPage(),
      routes: {
        '/identity': (context) => const IdentityVerificationPage(),
      },
    );
  }
}

class MainPage extends StatefulWidget {
  const MainPage({super.key});

  @override
  State<MainPage> createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  int _currentIndex = 0;

  final List<Widget> _pages = [
    const HomePage(),
    const WaitingPage(),
    const OfficeHallPage(),
    const ConsultationPage(),
    const ProfilePage(),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          _pages[_currentIndex],
          // 悬浮底部导航栏
          Positioned(
            bottom: 20,
            left: 12,
            right: 12,
            child: Container(
              height: 65,
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(32),
                boxShadow: [
                  BoxShadow(
                    color: Colors.black.withOpacity(0.08),
                    blurRadius: 8,
                    offset: const Offset(0, 2),
                  ),
                ],
              ),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                  _buildTabItem(0, Icons.home, '首页'),
                  _buildTabItem(1, Icons.schedule, '待办'),
                  _buildTabItem(2, Icons.business, '办事堂'),
                  _buildTabItem(3, Icons.chat, '消息'),
                  _buildTabItem(4, Icons.person, '我的'),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildTabItem(int index, IconData icon, String label) {
    bool isActive = _currentIndex == index;
    return GestureDetector(
      onTap: () {
        setState(() {
          _currentIndex = index;
        });
      },
      child: Container(
        padding: const EdgeInsets.symmetric(vertical: 6),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Icon(
              icon,
              color: isActive ? Colors.blue : Colors.grey[600],
              size: 22,
            ),
            const SizedBox(height: 2),
            Text(
              label,
              style: TextStyle(
                fontSize: 11,
                color: isActive ? Colors.blue : Colors.grey[600],
                fontWeight: isActive ? FontWeight.w600 : FontWeight.normal,
              ),
            ),
          ],
        ),
      ),
    );
  }
}

// 首页
class HomePage extends StatelessWidget {
  const HomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFFF1F2F8),
      body: SingleChildScrollView(
        child: Column(
          children: [
            // 顶部背景区域 - 使用背景图片 + 渐变遮罩
            Container(
              height: MediaQuery.of(context).size.height * 0.3, // 按需调整高度
              decoration: const BoxDecoration(
                image: DecorationImage(
                  image: AssetImage('assets/images/home_header_bg.png'),
                  fit: BoxFit.fill,
                ),
              ),
              child: Container(
                decoration: BoxDecoration(
                  gradient: LinearGradient(
                    begin: Alignment.topCenter,
                    end: Alignment.bottomCenter,
                    colors: [
                      Colors.transparent,
                      Colors.transparent,
                      Colors.white.withOpacity(0.1),
                      Colors.white.withOpacity(0.3),
                      Colors.white.withOpacity(0.6),
                      Colors.white,
                    ],
                    stops: const [0.0, 0.4, 0.6, 0.75, 0.85, 1.0],
                  ),
                ),
                child: SafeArea(
                  child: Column(
                    children: [
                      // 顶部搜索框和扫码图标
                      Padding(
                        padding: const EdgeInsets.fromLTRB(16, 12, 16, 16),
                        child: Row(
                          children: [
                            Expanded(
                              child: Container(
                                height: 44,
                                padding:
                                    const EdgeInsets.symmetric(horizontal: 16),
                                decoration: BoxDecoration(
                                  color: Colors.white.withOpacity(0.3),
                                  borderRadius: BorderRadius.circular(8),
                                ),
                                child: const Row(
                                  children: [
                                    Icon(Icons.search,
                                        color: Colors.white, size: 20),
                                    SizedBox(width: 8),
                                    Text(
                                      '请输入想搜索的功能/服务',
                                      style: TextStyle(
                                        color: Colors.white,
                                        fontSize: 15,
                                        fontWeight: FontWeight.w400,
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ),
                            const SizedBox(width: 12),
                            // 扫码图标
                            SizedBox(
                              height: 44,
                              width: 44,
                              child: Center(
                                child: SizedBox(
                                  height: 24,
                                  width: 24,
                                  child: CustomPaint(
                                    painter: ScanIconPainter(),
                                  ),
                                ),
                              ),
                            ),
                          ],
                        ),
                      ),

                      // 通知栏
                      Container(
                        margin: const EdgeInsets.fromLTRB(16, 0, 16, 20),
                        padding: const EdgeInsets.symmetric(
                            horizontal: 12, vertical: 10),
                        decoration: BoxDecoration(
                          color: const Color(0xFFFFF3CD),
                          borderRadius: BorderRadius.circular(8),
                        ),
                        child: Row(
                          children: [
                            Image.asset(
                              'assets/images/ring.png',
                              width: 20,
                              height: 20,
                              fit: BoxFit.contain,
                              // color: const Color(0xFFFF9500),
                            ),
                            const SizedBox(width: 8),
                            const Expanded(
                              child: Text(
                                '所得税综合所得汇算清缴管理办法》的解读',
                                style: TextStyle(
                                  fontSize: 14,
                                  color: Colors.black87,
                                  fontWeight: FontWeight.w500,
                                ),
                              ),
                            ),
                          ],
                        ),
                      ),
                      const SizedBox(height: 80), // 恢复原来的间距
                    ],
                  ),
                ),
              ),
            ),

            // 整体向上偏移的容器，包含2024卡片及以下所有内容
            Transform.translate(
              offset: const Offset(0, -80), // 整体向上偏移60px
              child: Column(
                children: [
                  // 2024综合所得年度汇算卡片
                  Container(
                    margin: const EdgeInsets.fromLTRB(4, 0, 4, 0),
                    height: 220,
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(14), // 调整为更小的12px圆角
                      // boxShadow: [
                      //   BoxShadow(
                      //     color: Colors.black.withOpacity(0.08),
                      //     blurRadius: 12,
                      //     offset: const Offset(0, 4),
                      //   ),
                      // ],
                      image: const DecorationImage(
                        image: NetworkImage(
                            'https://wcdn.etax.chinatax.gov.cn/appdist/banner/ndhs2024.png'),
                        fit: BoxFit.fill,
                      ),
                    ),
                    child: Padding(
                      padding: const EdgeInsets.fromLTRB(28, 20, 28, 20),
                      // 减少上下padding
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        mainAxisSize: MainAxisSize.min, // 重要：让Column只占用需要的空间
                        children: [
                          // 第一行：标题
                          const Text(
                            '2024综合所得年度汇算',
                            style: TextStyle(
                              fontSize: 20,
                              fontWeight: FontWeight.w700,
                              color: Color(0xFF1A1A1A),
                            ),
                          ),
                          const SizedBox(height: 12), // 减少间距

                          // 第二行：描述 - 让文字自然换行
                          const Text(
                            '已于6月30日结束，您可进入本专题页查看记录或补办年度汇算',
                            style: TextStyle(
                              fontSize: 16,
                              fontWeight: FontWeight.w400,
                              color: Color(0xFF666666),
                              height: 1.3, // 减少行高
                            ),
                          ),
                          const SizedBox(height: 21), // 减少间距

                          // 第三行：按钮 - 按照设计稿比例调整
                          SizedBox(
                            width: 185, // 占屏幕宽度的60%，相当于卡片的大部分宽度
                            child: Container(
                              padding: const EdgeInsets.symmetric(vertical: 12),
                              // 适中的垂直padding
                              decoration: BoxDecoration(
                                color: const Color(0xFF4285F4),
                                borderRadius: BorderRadius.circular(25), // 大圆角
                                boxShadow: [
                                  BoxShadow(
                                    color: const Color(0xFF4285F4)
                                        .withOpacity(0.3),
                                    blurRadius: 8,
                                    offset: const Offset(0, 4),
                                  ),
                                ],
                              ),
                              child: const Text(
                                '进入专题页',
                                textAlign: TextAlign.center, // 居中对齐
                                style: TextStyle(
                                  color: Colors.white,
                                  fontSize: 16, // 增大字号
                                  fontWeight: FontWeight.w600,
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),

                  // 重点服务推荐标题
                  Container(
                    margin: const EdgeInsets.fromLTRB(16, 0, 16, 8), // 恢复正常间距
                    child: const Align(
                      alignment: Alignment.centerLeft,
                      child: Text(
                        '重点服务推荐',
                        style: TextStyle(
                          fontSize: 18,
                          fontWeight: FontWeight.bold,
                          color: Colors.black87,
                        ),
                      ),
                    ),
                  ),

                  // 专项附加扣除蓝色卡片
                  Container(
                    margin: const EdgeInsets.fromLTRB(2, 0, 2, 0),
                    padding: const EdgeInsets.fromLTRB(20, 20, 20, 20),
                    // 只调整内边距
                    height: 210,
                    decoration: const BoxDecoration(
                      image: DecorationImage(
                        image: AssetImage('assets/images/important_bg.png'),
                        fit: BoxFit.fill,
                      ),
                    ),
                    child: Column(
                      children: [
                        Padding(
                          padding: const EdgeInsets.only(left: 10, right:16), // 左12像素，右8像素
                          child: Row(
                            children: [
                              Image.asset(
                                'assets/images/zxkc_icon.png',
                                height: 20,
                                fit: BoxFit.contain,
                              ),
                              const SizedBox(width: 8),
                              const Spacer(),
                              const Text(
                                '相关政策 >',
                                style: TextStyle(
                                  color: Colors.white,
                                  fontSize: 16,
                                ),
                              ),
                            ],
                          ),
                        ),
                        const SizedBox(height: 24), // 调整间距
                        const Align(
                          alignment: Alignment.center,
                          child: Text(
                            '若符合条件，您可点击下方填报',
                            style: TextStyle(
                              color: Colors.white,
                              fontSize: 14,
                            ),
                          ),
                        ),
                        const SizedBox(height: 24), // 调整间距
                        Container(
                          child: Row(
                            mainAxisSize: MainAxisSize.min,
                            children: [
                              Image.asset(
                                'assets/images/wb.png',
                                height: 50,
                                fit: BoxFit.fill,
                              ),
                            ],
                          ),
                        ),
                      ],
                    ),
                  ),

                  // 可滑动的服务卡片区域
                  Container(
                    margin: const EdgeInsets.only(top: 8),
                    height: 180, // 固定高度
                    child: PageView(
                      padEnds: false,
                      controller: PageController(viewportFraction: 0.3), // 每个卡片宽度 30%，支持滑动
                      children: List.generate(4, (index) {
                        return Container(
                          margin: const EdgeInsets.only(left: 16, right: 8),
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Expanded(
                                child: Padding(
                                  padding: const EdgeInsets.all(12),
                                  child: Column(
                                    crossAxisAlignment: CrossAxisAlignment.start,
                                    children: [
                                      Container(
                                        height: 40,
                                        width: 40,
                                        decoration: BoxDecoration(
                                          borderRadius: BorderRadius.circular(12), // logo 圆角
                                        ),
                                        child: Image.asset(
                                          'assets/images/home_icon_1.png', // logo
                                          fit: BoxFit.cover,
                                        ),
                                      ),
                                      SizedBox(height: 8),
                                      Container(
                                        height: 70, // 文字背景高度
                                        width: double.infinity,
                                        decoration: BoxDecoration(
                                          image: DecorationImage(
                                            image: AssetImage('assets/images/ip_bg.png'), // 文字背景
                                            fit: BoxFit.fill,
                                          ),
                                        ),
                                        child: Padding(
                                          padding: const EdgeInsets.all(8),
                                          child: Column(
                                            mainAxisAlignment: MainAxisAlignment.center,
                                            crossAxisAlignment: CrossAxisAlignment.start,
                                            children: [
                                              Text(
                                                '2024综合所得年度汇算',
                                                style: TextStyle(
                                                  color: Color(0xFF333333), // 深灰色
                                                  fontSize: 14,
                                                  fontWeight: FontWeight.w500,
                                                ),
                                              ),
                                              SizedBox(height: 4),
                                              Text(
                                                '已于6月30日结束，您可进入本专题查看往年记录',
                                                style: TextStyle(
                                                  color: Color(0xFF666666), // 较浅灰色
                                                  fontSize: 12,
                                                ),
                                              ),
                                            ],
                                          ),
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                              ),
                              Padding(
                                padding: const EdgeInsets.only(bottom: 12, left: 12, right: 12),
                                child: SizedBox(
                                  height: 40, // 固定按钮高度
                                  child: ElevatedButton(
                                    onPressed: () {},
                                    style: ElevatedButton.styleFrom(
                                      backgroundColor: Color(0xFF007AFF), // 蓝色按钮
                                      foregroundColor: Colors.white, // 白色文字
                                      shape: RoundedRectangleBorder(
                                        borderRadius: BorderRadius.circular(20),
                                      ),
                                      minimumSize: Size(double.infinity, 0), // 宽度充满
                                    ),
                                    child: Text('进入专题页'),
                                  ),
                                ),
                              ),
                            ],
                          ),
                        );
                      }),
                    ),
                  ),
                ],
              ),
            ),

            const SizedBox(height: 100), // 为悬浮导航栏留出空间
          ],
        ),
      ),
    );
  }
}

// 其他页面
class WaitingPage extends StatelessWidget {
  const WaitingPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('待办'),
        backgroundColor: Colors.blue,
        foregroundColor: Colors.white,
      ),
      body: const Center(
        child: Text('待办页面', style: TextStyle(fontSize: 18)),
      ),
    );
  }
}

class OfficeHallPage extends StatelessWidget {
  const OfficeHallPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('办事堂'),
        backgroundColor: Colors.blue,
        foregroundColor: Colors.white,
      ),
      body: const Center(
        child: Text('办事堂页面', style: TextStyle(fontSize: 18)),
      ),
    );
  }
}

class ConsultationPage extends StatelessWidget {
  const ConsultationPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('消息'),
        backgroundColor: Colors.blue,
        foregroundColor: Colors.white,
      ),
      body: const Center(
        child: Text('消息页面', style: TextStyle(fontSize: 18)),
      ),
    );
  }
}

class ProfilePage extends StatelessWidget {
  const ProfilePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('我的'),
        backgroundColor: Colors.blue,
        foregroundColor: Colors.white,
      ),
      body: const Center(
        child: Text('我的页面', style: TextStyle(fontSize: 18)),
      ),
    );
  }
}

class IdentityVerificationPage extends StatelessWidget {
  const IdentityVerificationPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('身份验证'),
        backgroundColor: Colors.blue,
        foregroundColor: Colors.white,
      ),
      body: const Center(
        child: Text('身份验证页面', style: TextStyle(fontSize: 18)),
      ),
    );
  }
}

// Custom scan icon painter
class ScanIconPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = Colors.white
      ..strokeWidth = 2.0
      ..style = PaintingStyle.stroke;

    final double cornerLength = size.width * 0.25;
    final double centerY = size.height / 2;

    // 左上角
    canvas.drawLine(
      Offset(0, cornerLength),
      const Offset(0, 0),
      paint,
    );
    canvas.drawLine(
      const Offset(0, 0),
      Offset(cornerLength, 0),
      paint,
    );

    // 右上角
    canvas.drawLine(
      Offset(size.width - cornerLength, 0),
      Offset(size.width, 0),
      paint,
    );
    canvas.drawLine(
      Offset(size.width, 0),
      Offset(size.width, cornerLength),
      paint,
    );

    // 左下角
    canvas.drawLine(
      Offset(0, size.height - cornerLength),
      Offset(0, size.height),
      paint,
    );
    canvas.drawLine(
      Offset(0, size.height),
      Offset(cornerLength, size.height),
      paint,
    );

    // 右下角
    canvas.drawLine(
      Offset(size.width - cornerLength, size.height),
      Offset(size.width, size.height),
      paint,
    );
    canvas.drawLine(
      Offset(size.width, size.height),
      Offset(size.width, size.height - cornerLength),
      paint,
    );

    // 中间横杠
    canvas.drawLine(
      Offset(size.width * 0.2, centerY),
      Offset(size.width * 0.8, centerY),
      paint,
    );
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) => false;
}
